﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Windows Forms Controls Class Library
 * Version 2.00
 * 
 * Toolbox Control Class
 *      Provides a UI element to act as a toobox similar to that of Visual Studio.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Adaptive.Foundation.Windows.Forms
{
    /// <summary>
    /// Provides a UI element to act as a toobox similar to that of Visual Studio.
    /// </summary>
    [ToolboxItem(true)]
    public partial class ToolboxControl : ToolboxControlBase  
    {
        /*--------------------------------------------------------------------------------
         * Public Event Definitions
         *------------------------------------------------------------------------------*/
        #region Public Event Definitions
        /// <summary>
        /// Occurs when the close button is clicked.
        /// </summary>
        public event CancelEventHandler ToolboxCloseRequest;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ToolboxControl()
            : base()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }
            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties  
        /// <summary>
        /// Gets or sets a value indicating whether the close button is displayed.
        /// </summary>
        /// <value>
        /// <b>true</b> to show the close button; otherwise, <b>false</b>.
        /// </value>
        [Browsable(true),
         DefaultValue(true),
         Description("Show or hide the close button.")]
        public bool ShowCloseButton
        {
            get { return ToolboxTitleBar.ShowCloseButton; }
            set
            {
                ToolboxTitleBar.ShowCloseButton = value;
                Invalidate();
            }
        }
        /// <summary>
        /// Gets or sets the text of the control.
        /// </summary>
        /// <value>The text associated with this control.</value>
        public override string Text
        {
            get
            {
                return ToolboxTitleBar.Text;
            }
            set
            {
                ToolboxTitleBar.Text = value;
                Invalidate();
            }
        }
        /// <summary>
        /// Gets or sets the font of the text displayed by the control.
        /// </summary>
        /// <value></value>
        /// <returns>The <see cref="T:System.Drawing.Font"/> to apply to the text displayed by the control. The default is the value of the <see cref="P:System.Windows.Forms.Control.DefaultFont"/> property.</returns>
        /// <PermissionSet>
        /// 	<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// 	<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>
        /// 	<IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// </PermissionSet>
        public override Font Font
        {
            get
            {
                return ToolboxTitleBar.Font;
            }
            set
            {
                ToolboxTitleBar.Font = value;
                Invalidate();
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the items contained within the control.
        /// </summary>
        protected override void AssignEventHandlers()
        {
            ToolboxTitleBar.ToolboxCloseRequest +=new CancelEventHandler(HandleToolboxCloseRequest);
            ToolboxTitleBar.RightClick +=new System.EventHandler(HandleControlRightClick);
        }
        /// <summary>
        /// Removes the event handlers for the items contained within the control.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
            ToolboxTitleBar.ToolboxCloseRequest -= new CancelEventHandler(HandleToolboxCloseRequest);
            ToolboxTitleBar.RightClick -= new System.EventHandler(HandleControlRightClick);
        }
        /// <summary>
        /// Raises the <see cref="E:ToolboxCloseRequest"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        protected virtual void OnToolboxCloseRequest(CancelEventArgs e)
        {
            if (ToolboxCloseRequest != null)
                ToolboxCloseRequest(this, e);
        }
        /// <summary>
        /// Resets the color value assignments for contained controls.
        /// </summary>
        protected override void ResetColors()
        {
            base.ResetColors();
            if (Created)
            {
                foreach (ToolboxCollapsiblePanel item in ContainerPanel.Controls)
                {
                    item.ItemBackColor = ItemBackColor;
                    item.ItemForeColor = ItemForeColor;
                    item.ItemHighlightBackColor = ItemHighlightBackColor;
                    item.ItemHighlightForeColor = ItemHighlightForeColor;
                    item.ItemBorderColor = ItemBorderColor;
                }
            }
            Invalidate();
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            ResetColors();
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Adds a new container panel to the toolbox.
        /// </summary>
        /// <param name="id">A name or ID to assign to the panel.</param>
        /// <param name="title">The text to display in the panel.</param>
        /// <returns>
        /// The <see cref="ToolboxCollapsiblePanel"/> instance added to the toolbox.
        /// </returns>
        public virtual ToolboxCollapsiblePanel AddPanel(string id, string title)
        {
            ToolboxCollapsiblePanel newPanel = null;          //Return value.

            newPanel = new ToolboxCollapsiblePanel();
            newPanel.Name = id;
            newPanel.Text = title;
            newPanel.ItemBackColor = ItemBackColor;
            newPanel.ItemBorderColor = ItemBorderColor;
            newPanel.ItemForeColor = ItemForeColor;
            newPanel.ItemHighlightBackColor = ItemHighlightBackColor;
            newPanel.ItemHighlightForeColor = ItemHighlightForeColor;
            newPanel.VisualState = CollapsiblePanelState.Collapsed;
            newPanel.Dock = DockStyle.Top;
            newPanel.RightClick +=new EventHandler(HandleControlRightClick);
            ContainerPanel.Controls.Add(newPanel);
            newPanel.BringToFront();

            return newPanel;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        /// <summary>
        /// Handles the toolbox close request.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void HandleToolboxCloseRequest(object sender, CancelEventArgs e)
        {
            OnToolboxCloseRequest(e);
        }
        /// <summary>
        /// Handles the control right click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void HandleControlRightClick(object sender, EventArgs e)
        {
            OnRightClick(e);
        }
        #endregion

    }
}
